<%extends"admin/base.html"%><%block content%>
<div class="layui-card">
  <div class="layui-form layui-card-header layuiadmin-card-header-auto">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-inline">
          <input type="text" name="title" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">分类</label>
        <div class="layui-input-inline">
          <select name="label">
            <option value="">请选择标签</option>
            <% for category in categories %>
            <option value="<< category.Id >>"><< category.name >></option>
            <% endfor %>
          </select>
        </div>
      </div>
      <div class="layui-inline">
        <button class="layui-btn" lay-submit lay-filter="LAY-app-contlist-search">
          <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
        </button>
      </div>
    </div>
  </div>

  <div class="layui-card-body">
    <div style="padding-bottom: 10px;">
      <button class="layui-btn layuiadmin-btn-list layui-btn-danger" data-type="batchdel">删除</button>
      <button class="layui-btn layuiadmin-btn-list layui-btn-warm" data-type="add">添加</button>
    </div>
    <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
    <script type="text/html" id="buttonTpl">
      <img src="{{ d.imgSrc }}" alt="{{ d.title }}"/>
    </script>
    <script type="text/html" id="table-content-list">
      <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
    </script>
  </div>
</div>
<%endblock%>
<%block script%>
    //监听搜索
    form.on('submit(LAY-app-contlist-search)', function(data){
      var field = data.field;

      //执行重载
      table.reload('LAY-app-content-list', {
        where: field,
        method:'post'
      });
    });

    var active = {
      batchdel: function(){
        var checkStatus = table.checkStatus('LAY-app-content-list')
        ,checkData = checkStatus.data; //得到选中的数据

        if(checkData.length === 0){
          return layer.msg('请选择数据');
        }

        layer.confirm('确定删除吗？', function(index) {
            $.each(checkData,function(index,item){
                 //执行 Ajax 后重载
                  $.ajax({
                    url: "<< url_for('admin.BlogDel') >>",
                    data:{Id:item.Id},
                        dataType:'json',
                    success: function (returnData) {
                        if (returnData.status == 1) {
                            layer.msg('已删除，'+item.title);
                        }
                        else {
                            layer.msg(item.title+'，'+returnData.info);
                        }
                     }
                  });
            });
            table.reload('LAY-app-content-list');
        });
      },
      add: function(){
        layer.open({
          type: 2
          ,title: '添加文章'
          ,content: "<< url_for('admin.BlogAdd') >>"
          ,maxmin: true
          ,area: ['750px', '600px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            //点击确认触发 iframe 内容中的按钮提交
            var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
            submit.click();
          }
        });
      }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
    table.render({
        elem: "#LAY-app-content-list",
        url: "<< url_for('admin.BlogList') >>",
        text: {
            none: '暂无相关数据', //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            error:'对不起，加载出现异常！'
        },
        method:'post',
        cols: [[{type: "checkbox", fixed: "left"}, {field: "Id", width: 100, title: "文章ID", sort: !0}, {
            field: "categoryName",
            title: "文章分类",
            minWidth: 50
        }, {field: "title", title: "文章标题"},{
            field: "createTime",
            title: "上传时间",
            sort: !0
        }, {field: "imgSrc", title: "封面", templet: "#buttonTpl", minWidth: 50, align: "center"}
          , {field: "readingVolume", title: "阅读量"},{
            title: "操作",
            minWidth: 150,
            align: "center",
            fixed: "right",
            toolbar: "#table-content-list"
        }]],
        page: !0,
        limit: 15,
        limits: [10, 15, 20, 25, 30]
    }), table.on("tool(LAY-app-content-list)", function (t) {
        var e = t.data;
        "del" === t.event ? layer.confirm("确定删除此文章？", function (er) {
            layer.close(er)
            $.ajax({
            url: "<< url_for('admin.BlogDel') >>",
            data:{Id:e.Id},
             dataType:'json',
            success: function (returnData) {
                if (returnData.status == 1) {
                    table.reload('LAY-app-content-list');
                    layer.msg('已删除，'+e.title);
                    t.del()
                }
                else {
                    layer.msg(e.title+'，'+returnData.info);
                }
             }
          });
        }) : "edit" === t.event && layer.open({
            type: 2,
            title: "编辑文章",
            content: "/admin/details/edit/" + e.Id,
            maxmin: !0,
            area: ['750px', '600px'],
            btn: ["确定", "取消"],
            yes: function (e, i) {
                 var l = window["layui-layer-iframe" + e],
                a = i.find("iframe").contents().find("#layuiadmin-app-form-edit");
                a.click();
            }
        })
    })

    $('.layui-nav-child dd').removeClass('layui-this');
    $('#view_admin_blog_list').addClass('layui-this');
    $('.layui-nav-tree li').removeClass('layui-nav-itemed');
    $('#admin_blog_nav').addClass('layui-nav-itemed');
<%endblock%>